package client.ui.component.navigation;

import client.ui.component.button.FlatButton;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;

/**
 * 单个导航条目按钮
 */
public class NavigationBarItem extends FlatButton {
    protected NavigationBarGroup navigationBars;
    public NavigationBarItem(String text, Icon icon, NavigationBarGroup navigationBarGroup) {
        super(text, icon);
        this.navigationBars = navigationBarGroup;
        navigationBars.add(this);
    }

    public NavigationBarItem(String text, Icon icon, Color normalColor, Color hoverColor, Color pressedColor, Color normalTextColor, Color hoverTextColor, Color pressedTextColor,NavigationBarGroup navigationBarGroup) {
        super(text, icon, normalColor, hoverColor, pressedColor, normalTextColor, hoverTextColor, pressedTextColor);
        this.navigationBars = navigationBarGroup;
        navigationBars.add(this);
    }

    /**
     * @Descripion 重绘，添加选中后的效果，选中后装饰采用normalTextColor
     * @return void
     * @author 孔明浩
     * @Date 2020/3/31
     */
    @Override
    public void paint(Graphics g) {

        super.paint(g);
        if (selected){
            Graphics2D graphics2D = (Graphics2D) g.create();
            graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            GradientPaint paint = new GradientPaint(0, 0, pressedTextColor,0,0,pressedTextColor);
            graphics2D.setPaint(paint);
            graphics2D.fillRect(0,0,5,getHeight());
            GradientPaint paint1 = new GradientPaint(0, 0, normalColor,0,0,normalColor);
            graphics2D.setPaint(paint1);
            graphics2D.drawRoundRect(2,getHeight() / 4,1,1,1,1);
            graphics2D.drawRoundRect(2,getHeight() / 2,1,1,1,1);
            graphics2D.drawRoundRect(2,getHeight() * 3 / 4,1,1,1,1);
            graphics2D.dispose();
        }
    }
    /**
     * @Description 调用按钮组的设置其他按钮非选中状态
     * @return void
     * @author 孔明浩
     * @Date 2020/3/31
     */
    @Override
    public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);
        if (e.getButton() == MouseEvent.BUTTON1){
            selected = true;
            this.setBackground(this.pressedColor);
            navigationBars.setSelected(this.getModel(),true);
        }
    }

    public NavigationBarGroup getNavigationBars() {
        return navigationBars;
    }

    public void setNavigationBars(NavigationBarGroup navigationBars) {
        this.getModel().setGroup(navigationBars);
        this.navigationBars = navigationBars;
    }
}
